package com.teng.mylibrary.util

import androidx.databinding.ObservableList
import androidx.recyclerview.widget.RecyclerView


/**
 * Description:
 * Author:       liuteng
 * CreateDate:   2022/4/23 11:15
 * UpdateRemark: 更新说明
 */
object AdapterFactory {

    fun getListChangedCallback(adapter: RecyclerView.Adapter<*>): ObservableList.OnListChangedCallback<ObservableList<*>> {
        return object : ObservableList.OnListChangedCallback<ObservableList<*>>() {
            override fun onChanged(observableList: ObservableList<*>) {
                adapter.notifyDataSetChanged()
            }

            override fun onItemRangeChanged(observableList: ObservableList<*>?, i: Int, i1: Int) {
                adapter.notifyDataSetChanged()
            }

            override fun onItemRangeInserted(observableList: ObservableList<*>?, i: Int, i1: Int) {
                adapter.notifyItemRangeInserted(adapter.itemCount, i1)
            }

            override fun onItemRangeMoved(
                observableList: ObservableList<*>?,
                i: Int,
                i1: Int,
                i2: Int
            ) {
                adapter.notifyDataSetChanged()

                /*  if (i2 == 1) {
                      adapter.notifyItemMoved(i, i1)
                  } else {
                      adapter.notifyDataSetChanged()
                  }*/
            }

            override fun onItemRangeRemoved(observableList: ObservableList<*>?, i: Int, i1: Int) {
                adapter.notifyItemRangeRemoved(i, i1)
                adapter.notifyItemRangeChanged(i, adapter.itemCount - i)
            }
        }
    }
}